package com.linb.pan.server.modules.user.controller;


import com.linb.pan.core.response.R;
import com.linb.pan.server.common.annotation.LoginIgnore;
import com.linb.pan.server.common.utils.UserIdUtil;
import com.linb.pan.server.modules.user.context.*;
import com.linb.pan.server.modules.user.converter.UserConverter;
import com.linb.pan.server.modules.user.po.*;
import com.linb.pan.server.modules.user.service.IUserService;
import com.linb.pan.server.modules.user.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("user")
@Api(tags = "用户模块")
public class UserController {

    @Autowired
    private UserConverter userConverter;

    @Autowired
    private IUserService userService;

    @ApiOperation(
            value = "用户注册接口",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("register")
    @LoginIgnore
    public R register(@Validated @RequestBody UserRegisterPO userRegisterPO) {
        //1.PO转上下文
        UserRegisterContext userRegisterContext = userConverter.userRegisterPO2UserRegisterContext(userRegisterPO);
        //2.注册
        Long userId = userService.register(userRegisterContext);
        //3.返回用户id
        return R.data(userId);
    }

    @PostMapping("login")
    @ApiOperation(value = "用户登录接口")
    @LoginIgnore
    public R login(@Validated @RequestBody UserLoginPO userLoginPO) {
        //1.PO转上下文
        UserLoginContext userLoginContext = userConverter.userLoginPOToUserLoginContext(userLoginPO);
        //2.登录
        String accessToken = userService.login(userLoginContext);
        //3.返回token
        return R.data(accessToken);
    }

    @PostMapping("exit")
    @ApiOperation(value = "退出登录")
    public R exit() {
        userService.exit(UserIdUtil.get());
        return R.success();
    }

    @PostMapping("username/check")
    @ApiOperation(value = "校验用户名")
    @LoginIgnore
    public R checkUsername(@Validated @RequestBody CheckUsernamePO po) {
        CheckUsernameContext usernameContext = userConverter.checkUsernamePOToCheckUsernameContext(po);
        String question = userService.checkUsername(usernameContext);
        return R.data(question);
    }

    @PostMapping("answer/check")
    @ApiOperation(value = "校验密保答案")
    @LoginIgnore
    public R checkAnswer(@Validated @RequestBody CheckAnswerPO po) {
        CheckAnswerContext checkAnswerContext = userConverter.checkAnswerPOToCheckAnswerContext(po);
        String token = userService.checkAnswer(checkAnswerContext);
        return R.data(token);
    }

    @PostMapping("password/reset")
    @ApiOperation(value = "重置密码")
    @LoginIgnore
    public R resetPassword(@Validated @RequestBody ResetPasswordPO po) {
        ResetPasswordContext context = userConverter.resetPasswordPOToResetPasswordContext(po);
        userService.resetPassword(context);
        return R.success();
    }

    @PostMapping("password/change")
    @ApiOperation(value = "在线修改密码")
    public R changePassword(@Validated @RequestBody ChangePasswordPO po) {
        ChangePasswordContext context = userConverter.changePasswordPOToChangePasswordContext(po);
        context.setUserId(UserIdUtil.get());
        userService.changePassword(context);
        return R.success();
    }

    @GetMapping
    @ApiOperation(value = "查询用户基本信息")
    public R<UserInfoVO> getUserBaseInfo() {
        UserInfoVO vo = userService.getUserBaseInfo(UserIdUtil.get());
        return R.data(vo);
    }


}
